/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.lightbox;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.link.ExternalLink;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * @author Nicolas Giard
 */
@SuppressWarnings("serial")
public class JQLightboxLink extends ExternalLink implements IJQPluginComponent
{
	private String classAttr;
	private String albumRel;
	private String title;

	private JQLightboxPlugin plugin;

	/** JQuery Lightbox widget options */
	private JQLightboxOptions options = new JQLightboxOptions();

	/**
	 * @param id
	 * @param href
	 * @param classAttr
	 * @param albumRel
	 * @param label
	 * @param key
	 */
	public JQLightboxLink(String id, String href, String classAttr, String albumRel, String label, String key)
	{
		super(id, href, label);
		this.classAttr = classAttr;
		this.albumRel = albumRel;
		this.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQLightboxPlugin.DEFAULT_KEY;
		}
		// -- Checks plugin existence
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQLightboxPlugin(key);
		}
		this.plugin = (JQLightboxPlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(key).attachComponentForDocumentReady(this);
	}

	/**
	 * @param id
	 * @param href
	 * @param classAttr
	 * @param albumRel
	 * @param label
	 */
	public JQLightboxLink(String id, String href, String classAttr, String albumRel, String label)
	{
		this(id, href, classAttr, albumRel, label, null);
	}

	/**
	 * @param id
	 * @param href
	 * @param classAttr
	 * @param albumRel
	 */
	public JQLightboxLink(String id, String href, String classAttr, String albumRel)
	{
		this(id, href, classAttr, albumRel, null, null);
	}

	/**
	 * @param id
	 * @param href
	 * @param classAttr
	 */
	public JQLightboxLink(String id, String href, String classAttr)
	{
		this(id, href, classAttr, "album", null, null);
	}

	/**
	 * 
	 * @return String
	 */
	public String getTitle()
	{
		return title;
	}

	/**
	 * @param title
	 */
	public void setTitle(String title)
	{
		this.title = title;
	}

	@Override
	protected void onComponentTag(ComponentTag tag)
	{
		super.onComponentTag(tag);

		if (getModel() != null)
		{
			tag.put("class", this.classAttr);
			tag.put("rel", this.albumRel);
			if (title != null && !title.equals(""))
			{
				tag.put("title", this.title);
			}
		}
	}

	public JQLightboxPlugin getPlugin()
	{
		return this.plugin;
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQLightboxPlugin) plugin;
	}

	public JQLightboxOptions getOptions()
	{
		return this.options;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQLightboxOptions.class))
		{
			this.options = (JQLightboxOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQLightboxLink.class, options.getClass(), JQLightboxOptions.class);
		}
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return "$('#" + this.getMarkupId() + "')." + this.options.getDocumentReadyOptions();
	}

	public String getAfterAjaxUpdateScript()
	{
		return getWidgetReadyScript(true);
	}
}